package com.jswlw.controller.admin.biz;

import com.github.pagehelper.Page;
import com.jswlw.common.BaseException;
import com.jswlw.common.ResponseStatus;
import com.jswlw.common.utils.StringUtils;
import com.jswlw.controller.BaseController;
import com.jswlw.entity.base.User;
import com.jswlw.entity.biz.DevFilter;
import com.jswlw.entity.biz.DevInfo;
import com.jswlw.entity.biz.Distributor;
import com.jswlw.entity.biz.log.LogDevOpt;
import com.jswlw.service.base.UserService;
import com.jswlw.service.biz.DevInfoService;
import com.jswlw.service.biz.DistributorService;
import com.jswlw.service.biz.log.LogDevOptService;
import com.jswlw.web.manager.SessionAdminManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴维斌 E-mail:wuweibin0817@163.com
 * @version 创建时间：${date} ${time}
 */
@Controller
@RequestMapping("/admin/biz/devOpt")
public class AdminDevOptController extends BaseController {

    @Autowired
    LogDevOptService logDevOptService;
    @Autowired
    UserService userService;
    @Autowired
    DistributorService distributorService;
    @Autowired
    DevInfoService devInfoService;

    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("biz/devOpt/index");
        return modelAndView;
    }

    @RequestMapping("/list")
    @ResponseBody
    public Object list(@RequestBody LogDevOpt filter) {
        if (Objects.nonNull(SessionAdminManager.getCurrentUser().getDistributor())) {
            Distributor curr = SessionAdminManager.getCurrentUser().getDistributor();
            DevInfo devInfoFilter = new DevInfo();
            devInfoFilter.setDistributorIds(curr.getChildrenIds());
            List<DevInfo> devInfoList = devInfoService.queryList(devInfoFilter);
            if (StringUtils.isNotEmpty(filter.getImei())) {
                boolean contains = false;
                for (DevInfo devInfo : devInfoList) {
                    if (devInfo.getImei().equals(filter.getImei())) {
                        contains = true;
                    }
                }
                if (!contains) {
                    BaseException.sneakyThrows(ResponseStatus.BUSINESS_ERROR.getCode(), "数据不存在");
                }
            }

            if (StringUtils.isNotEmpty(filter.getDevId())) {
                boolean contains = false;
                for (DevInfo devInfo : devInfoList) {
                    if (devInfo.getId().equals(filter.getDevId())) {
                        contains = true;
                    }
                }
                if (!contains) {
                    BaseException.sneakyThrows(ResponseStatus.BUSINESS_ERROR.getCode(), "数据不存在");
                }
            }
            if (StringUtils.isEmpty(filter.getImei()) && StringUtils.isEmpty(filter.getDevId())) {
                List<String> devIdList = new ArrayList<>();
                for (DevInfo devInfo : devInfoList) {
                    devIdList.add(devInfo.getId());
                }
                devIdList.add("1232411");
                filter.setDevIdList(devIdList);
            }
        }

        Page<LogDevOpt> page = logDevOptService.queryPage(filter);
        List<LogDevOpt> logDevOptList = page.getResult();
        logDevOptList.forEach(logDevOpt -> {
            User user = userService.find4Id(logDevOpt.getCreateUserId());
            if (Objects.nonNull(user)) {
                if (Objects.nonNull(user.getUserType()) && user.getUserType().equals(2)) {
                    Distributor distributor = distributorService.find4Id(Integer.parseInt(user.getLoginName()));
                    if (Objects.nonNull(distributor)) {
                        logDevOpt.setCreateUserName(distributor.getName());
                    }
                } else {
                    logDevOpt.setCreateUserName(user.getNickName());
                }
            }
        });
        return pageResult(page.getResult(), page.getTotal());
    }
}
